<?php 
/**
	Manage account contains the checkers (for adding new account in DB)
*/
require_once('Class/connexionDB.Class.php');

Class account 
{
	var $account = array();

	/**
		Is valid Email, check if the email given is not already in DB
	*/
	function isValidEmail($email)
	{
		$db = new connexionDB();

		if (isset($email) && $email != '' && filter_var($email, FILTER_VALIDATE_EMAIL) && $db->isMailExist($email) == false)
		{
			return true;
		}
		return false;
	}

	/**
		Is valid password, check if the 2 passwords given are the same
	*/
	function isValidPassword($password, $passwordRepeat)
	{
		if (isset($passwordRepeat) && $passwordRepeat != '' && isset($password) && $password != '' && $password == $passwordRepeat)
		{
			return true;
		}
		return false;
	}

	/**
		Is valid account type, 1 == Admin / 2 == Manager / 3 == CQ
	*/
	function isValidAccountType($id)
	{
		if (isset($id) && $id >= 1 && $id < 4)
		{
			return true;
		}
		return false;
	}

	/**
		Return an array of list of group 
	*/
	function listedGroup($data)
	{
		$groups = array();
		foreach ($data as $key => $value)
		{
			if (strpos($key, 'group') !== false)
			{
				$groups[] = $value;
			}
		}
		return $groups;
	}


	/**
		Add new account in DB
	*/
	function add($newAccount)
	{
		$db = new connexionDB();

		$db->addNewAccount(	$newAccount['firstname'], 
							$newAccount['lastname'], 
							$newAccount['id_account_type'], 
							$newAccount['email'], 
							$newAccount['password'], 
							$newAccount['groups'], 
							$newAccount['country'],
							$newAccount['team']);
	}

	/**
		Get all the data from the table `account` with the email given
	*/
	static function getDataAssociateToThisEmail($email)
	{
		$db = new connexionDB();

		return $db->getDataFromEmail($email);
	}

	/**
		Update data in the table account
	*/
	static function update($updatedAccount)
	{
		$db = new connexionDB();

		$db->updateAccount($updatedAccount);
	}

	static function updatePassword($email, $password)
	{
		$db = new connexionDB();

		$db->updatePassword($email, $password);
	}

	/**
		Delete the account associated to the email given
	*/
	static function deleteAccount($email)
	{
		$db = new connexionDB();

		$db->deleteAccount($email);
	}
}	

?>